package org.komarichin.google.mail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Feb 21, 2010
 * Time: 1:34:20 AM
 */
public class MailServiceImpl implements IMailService {

    private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);

    public void sendMessageToAdmins(String msgBody) {
        log.debug("send mail message to admin:{}", msgBody);
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);


        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("michael.komarichin@gmail.com"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("admin@.", "Mr. Komarichyn"));
            msg.setSubject("App Engine has been send report about working komarichyn application");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (AddressException e) {
            // ...
            log.error(e.getMessage(), e);
        } catch (MessagingException e) {
            // ...
            log.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        }
    }
}
